package com.sunshine.lnuplus.view

import android.app.DatePickerDialog
import android.content.Context
import android.content.Intent
import android.graphics.Color
import android.os.Bundle
import android.view.*
import android.widget.ImageButton
import android.widget.SeekBar
import android.widget.TextView
import androidx.fragment.app.DialogFragment
import com.sunshine.lnuplus.R
import com.sunshine.lnuplus.callback.ChooseWeekCallBack
import com.sunshine.lnuplus.model.InitData
import com.sunshine.lnuplus.ui.activity.TimetableSettingActivity
import com.sunshine.lnuplus.utils.DateUtils
import es.dmoral.toasty.Toasty
import kotlinx.android.synthetic.main.dialog_choose_week.*
import kotlinx.android.synthetic.main.dialog_choose_week.view.*

class ChooseWeekDialog : DialogFragment(){

    companion object{
        lateinit var callBack: ChooseWeekCallBack
        @JvmStatic
        fun newInstance(
            thisWeek: Int,
            callBack: ChooseWeekCallBack
        ) : ChooseWeekDialog{
            this.callBack = callBack
            val bundle = Bundle()
            bundle.putInt("this_week", thisWeek)
            val chooseWeekDialog = ChooseWeekDialog()
            chooseWeekDialog.arguments = bundle
            return chooseWeekDialog
        }
    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        val view = inflater.inflate(R.layout.dialog_choose_week, container, false)
        if(context!!.getSharedPreferences("com.sunshine.lnuplus_preferences", Context.MODE_PRIVATE).getBoolean("night", false)){
            view.choose_week_layout.setBackgroundColor(Color.BLACK)
            view.textView_week.setTextColor(Color.WHITE)
        }

        view.findViewById<TextView>(R.id.textView_week).text = "第${arguments!!.getInt("this_week")}周"
        view.findViewById<SeekBar>(R.id.seekBar_choose_week).apply {
            max = InitData.lessonStyle.weekCount - 1
            progress = arguments!!.getInt("this_week") - 1
            setOnSeekBarChangeListener(object : SeekBar.OnSeekBarChangeListener{
                override fun onProgressChanged(seekBar: SeekBar?, progress: Int, fromUser: Boolean) {
                    view.findViewById<TextView>(R.id.textView_week).text = "第${progress + 1}周"
                }

                override fun onStartTrackingTouch(seekBar: SeekBar?) {

                }

                override fun onStopTrackingTouch(seekBar: SeekBar?) {
                    callBack.onPress(seekBar!!.progress)
                    dialog?.dismiss()
                }

            })
        }
        val params = dialog?.window?.attributes
        params?.apply {
            gravity = Gravity.BOTTOM
            windowAnimations = R.style.bottom_animation
        }
        dialog?.window?.setBackgroundDrawableResource(R.drawable.dialog_weather_corner)

        if(savedInstanceState != null){
            //必须开启新线程否则不能执行
            object : Thread(){
                override fun run(){
                    dialog?.dismiss()
                }
            }.start()
        }

        return view
    }

    override fun onStart() {
        super.onStart()
        dialog?.window?.setLayout(activity!!.windowManager.defaultDisplay.width,
            WindowManager.LayoutParams.WRAP_CONTENT
        )
    }

    override fun onSaveInstanceState(outState: Bundle) {
        super.onSaveInstanceState(outState)
        outState.putBoolean("hasData", true)
    }
}